package com.xp.basic.heima.nio.netty.simpledemo.worker;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

import java.net.InetSocketAddress;

public class TestNettyWorkerService {
    public static void main(String[] args) throws InterruptedException {
        ChannelFuture channelFuture = new ServerBootstrap()
                //这里指定的意思就是第一个 eventloop 当boss 只管 accept 事件
                // 第二个 eventloop 当 worker 只管 read 和 write 事件
                .group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
                //指定 channel 实现类
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ssc) throws Exception {
                        // 这是什么管道呢？处理消息的管道？这个 StringDecoder handle 就是将字节流解码
                        ssc.pipeline().addLast(new StringDecoder());
                        // 这是入栈消息处理流
                        ChannelPipeline pipeline = ssc.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
                                System.out.println(Thread.currentThread().getName() + " s = " + s);
                            }
                        });
                    }
                })
                .bind(new InetSocketAddress(8080));
        //获取得到的channel
        Channel channel = channelFuture.sync().channel();
    }
}
